//
//  SAInventoryListVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/6/17.
//

#import "SAInventoryListVC.h"
#import "HDDatePickerView.h"
#import "SAInventoryListCell.h"
#import "SAInventoryListDetailVC.h"
#import "SAInventoryListModel2.h"
#import "SAStockInvoiceListAPI.h"
#import "SASystemStoreStoreListAPI.h"
#import "SAStoreModel.h"
#import "BubbleTableView.h"
#import "GFAlertView.h"
#import "SAStockCheckHandleAPI.h"
#import "SAUpdateInventoryVC.h"

@interface SAInventoryListVC ()<UITableViewDelegate, UITableViewDataSource, HTTPAPIDelegate>

@property (nonatomic, strong) UIView *topContentView;
@property (nonatomic, strong) UILabel *startLab;
@property (nonatomic, strong) UILabel *startDateLab;
@property (nonatomic, strong) UILabel *endLab;
@property (nonatomic, strong) UILabel *endDateLab;
@property (nonatomic, strong) UIImageView *iconImageV;
@property (nonatomic, strong) UIButton *startDateBtn;
@property (nonatomic, strong) UIButton *endDateBtn;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *tipView;
@property (nonatomic, strong) SAStockInvoiceListAPI *stockInvoiceListAPI;
@property (nonatomic, strong) NSMutableArray<SAInventoryListModel2 *> *dataMarr;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;

@property (nonatomic, strong) UIView *shopView;
@property (nonatomic, strong) UILabel *allShopLab;
@property (nonatomic, strong) UIImageView *allowImageV2;

@property (nonatomic, strong) UILabel *statusLab;
@property (nonatomic, strong) UIImageView *statusImageV;

@property (nonatomic, strong) SASystemStoreStoreListAPI *systemStoreStoreListAPI;
@property (nonatomic, strong) NSMutableArray<SAStoreModel *> *storeList;
@property (nonatomic, strong) BubbleTableView *storeBubbleView;
@property (nonatomic, assign) NSInteger storeRow;

@property (nonatomic, strong) BubbleTableView *statusBubbleView;
@property (nonatomic, assign) NSInteger statusRow;
@property (nonatomic, strong) SAStockCheckHandleAPI *stockCheckHandleAPI;

@end

@implementation SAInventoryListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    if (self.stockInvoiceListAPI == api) {
        if (self.currentPage == 1) {
            self.dataMarr = [SAInventoryListModel2 mj_objectArrayWithKeyValuesArray:data[@"data"][@"records"]];
        }else {
            for (NSDictionary *dict in (NSArray *)data[@"data"][@"records"]) {
                SAInventoryListModel2 *model = [SAInventoryListModel2 mj_objectWithKeyValues:dict];
                [self.dataMarr addObject:model];
            }
        }
        [self.tableView reloadData];
        self.tipView.hidden = self.dataMarr.count > 0;
    }else if (self.systemStoreStoreListAPI == api) {
        self.storeList = [SAStoreModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
        for (NSInteger i = 0; i < self.storeList.count; i++) {
            if ([SAValueManger shareInStance].userInfo.merchantId == self.storeList[i].merchantId) {
                self.storeRow = i;
            }
        }
        [self showStoreList];
    }else if (self.stockCheckHandleAPI == api) {
        self.currentPage = 1;
        self.stockInvoiceListAPI.currentPage = 1;
        [self loadData];
        [Prompt popPromptViewWithMsg:@"盘点处理成功" duration:2];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataMarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAInventoryListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAInventoryListCell" forIndexPath:indexPath];
    cell.model = self.dataMarr[indexPath.section];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SAInventoryListModel2 *model = self.dataMarr[indexPath.section];
    if ([model.checkStatus isEqualToString:@"0"]) {
        SAUpdateInventoryVC *pushVC = [[SAUpdateInventoryVC alloc] initWithCheckFlowNo:model.checkFlowNo];
        WeakSelf
        pushVC.refreshBlock = ^{
            weakSelf.currentPage = 1;
            weakSelf.stockInvoiceListAPI.currentPage = 1;
            [weakSelf loadData];
        };
        [self.navigationController pushViewController:pushVC animated:YES];
    }else {
        SAInventoryListDetailVC *pushVC = [[SAInventoryListDetailVC alloc] initWithCheckFlowNo:model.checkFlowNo];
        [self.navigationController pushViewController:pushVC animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(140);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

#pragma mark - event
- (void)clickStartDateBtn:(UIButton *)btn {
    WeakSelf
    HDDatePickerView *pickerView = [[HDDatePickerView alloc] initWithSlectedDate:self.startDateLab.text];
    [pickerView show];
    pickerView.selectDateBlock = ^(NSString *selectedDate) {
                NSDate *date1 = [self.dateFormatter dateFromString:selectedDate];
                NSDate *date2 = [self.dateFormatter dateFromString:weakSelf.endDateLab.text];
                if ([date1 compare:date2] == NSOrderedDescending) {
                    [Prompt popPromptViewWithMsg:@"开始日期不能大于截止日期" duration:2];
                }else {
                    weakSelf.startDateLab.text = selectedDate;
                    weakSelf.currentPage = 1;
                    weakSelf.stockInvoiceListAPI.startTime = selectedDate;
                    [weakSelf loadData];
                }
    };
}

- (void)clickEndDateBtn:(UIButton *)btn {
    WeakSelf
    HDDatePickerView *pickerView = [[HDDatePickerView alloc] initWithSlectedDate:self.endDateLab.text];
    [pickerView show];
    pickerView.selectDateBlock = ^(NSString *selectedDate) {
                NSDate *date1 = [self.dateFormatter dateFromString:selectedDate];
                NSDate *date2 = [self.dateFormatter dateFromString:weakSelf.startDateLab.text];
                if ([date1 compare:date2] == NSOrderedAscending) {
                    [Prompt popPromptViewWithMsg:@"截止日期不能小于开始日期" duration:2];
                }else {
                    weakSelf.endDateLab.text = selectedDate;
                    weakSelf.currentPage = 1;
                    weakSelf.stockInvoiceListAPI.endTime = selectedDate;
                    [weakSelf loadData];
                }
    };
}

-(void)loadData {
    self.stockInvoiceListAPI.currentPage = self.currentPage;
    [self.stockInvoiceListAPI sendRequestToServerWithActivityViewAndMask:GET];
}

-(void)clickedAllowBtn2 {
    
    NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
    NSString *crossStoreCheckStockOrder = [NSString stringWithFormat:@"%@",menuList[3][@"perms"][@"crossStoreCheckStockOrder"]];
    if ([crossStoreCheckStockOrder isEqualToString:@"1"]) {
        [Prompt popPromptViewWithMsg:@"您没有跨店查盘点单权限,请联系店长添加" duration:2];
        return;
    }
    
    if (self.storeList.count > 0) {
        [self showStoreList];
    }else {
        self.systemStoreStoreListAPI.returnAll = @"false";
        [self.systemStoreStoreListAPI sendRequestToServerWithActivityViewAndMask:GET];
    }
}

-(void)showStoreList{
    NSMutableArray *marr = [NSMutableArray array];
    for (SAStoreModel *model in self.storeList) {
        if (model.storeName.length == 0) {
            model.storeName = @"";
        }
        [marr addObject:model.storeName];
    }
    NSInteger height = marr.count * 50 + 10;
    if (height > 190) {
        height = 190;
    }
    NSArray *arr = nil;
    UIView *window = [UIApplication sharedApplication].keyWindow;
    if (!self.storeBubbleView) {
        self.storeBubbleView = [[BubbleTableView alloc] initWithLocation:(BubbleLocationDown) bubbleAlignment:(BubbleAlignmentRight) width:120 height:height cellClickBlock:^(NSInteger row, NSString * _Nonnull rowString, NSArray *fromDataArray) {
            self.storeRow = row;
            self.stockInvoiceListAPI.merchantId = @(self.storeList[row].merchantId).stringValue;
            self.allShopLab.text = rowString;
            self.currentPage = 1;
            self.stockInvoiceListAPI.currentPage = 1;
            [self loadData];
        } hideBlock:^{
            self.storeBubbleView = nil;
        }];
        [self.storeBubbleView showWithTargetView:self.allowImageV2 maskSuperView:window];
        [self.storeBubbleView updateBubble:marr fromDataArray:arr];// 更新气泡弹框
        self.storeBubbleView.bubbleCellHeight = 50;
        self.storeBubbleView.selectRow = self.storeRow;
        self.storeBubbleView.isBubbleScrollEnabled = YES;
        self.storeBubbleView.bubbleCellTextAlignment = NSTextAlignmentCenter;
    }
}
    
-(void)clickedStatusBtn{
    NSMutableArray *marr = [NSMutableArray arrayWithObjects:@"全部", @"未处理", @"已处理", @"已作废",nil];
    NSInteger height = marr.count * 50 + 10;
    if (height > 190) {
        height = 190;
    }
    NSArray *arr = nil;
    UIView *window = [UIApplication sharedApplication].keyWindow;
    if (!self.statusBubbleView) {
        self.statusBubbleView = [[BubbleTableView alloc] initWithLocation:(BubbleLocationDown) bubbleAlignment:(BubbleAlignmentRight) width:120 height:height cellClickBlock:^(NSInteger row, NSString * _Nonnull rowString, NSArray *fromDataArray) {
            self.statusRow = row;
            if (row == 0) {
                self.stockInvoiceListAPI.checkStatus = @"";
            }else {
                self.stockInvoiceListAPI.checkStatus = @(row - 1).stringValue;
            }
            self.statusLab.text = rowString;
            self.currentPage = 1;
            self.stockInvoiceListAPI.currentPage = 1;
            [self loadData];
        } hideBlock:^{
            self.statusBubbleView = nil;
        }];
        [self.statusBubbleView showWithTargetView:self.statusImageV maskSuperView:window];
        [self.statusBubbleView updateBubble:marr fromDataArray:arr];// 更新气泡弹框
        self.statusBubbleView.bubbleCellHeight = 50;
        self.statusBubbleView.selectRow = self.statusRow;
        self.statusBubbleView.isBubbleScrollEnabled = YES;
        self.statusBubbleView.bubbleCellTextAlignment = NSTextAlignmentCenter;
    }
}

-(void)clickedDisposeBtn {
    GFAlertView *alertView = [GFAlertView alertViewWithMsg:@"处理提醒" detailMsg:@"确定处理盘点商品，并以云端最新库存计算盈亏数?" knowBtnBlock:^(UIButton *knowBtn) {
        [knowBtn addTarget:self action:@selector(clickedDisposeBtn2) forControlEvents:UIControlEventTouchUpInside];
    }];
    [alertView showWithAnimation];
}

-(void)clickedDisposeBtn2 {
    [self.stockCheckHandleAPI sendRequestToServerWithActivityViewAndMask:POST];
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"盘点单据"];
    [self setBgImage:nil];
    
    UIButton *disposeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    disposeBtn.frame = CGRectMake(0, 0, kScaleNum(90), kScaleNum(44));
    [disposeBtn setTitle:@"盘点处理" forState:UIControlStateNormal];
    disposeBtn.titleLabel.font = kFont(18);
    [disposeBtn addTarget:self action:@selector(clickedDisposeBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addRightViews:@[disposeBtn]];
    
    [self.view addSubview:self.topContentView];
    [self.topContentView addSubview:self.startLab];
    [self.topContentView addSubview:self.startDateLab];
    [self.topContentView addSubview:self.endLab];
    [self.topContentView addSubview:self.endDateLab];
    [self.topContentView addSubview:self.iconImageV];
    [self.topContentView addSubview:self.startDateBtn];
    [self.topContentView addSubview:self.endDateBtn];
    
    [self.topContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kScaleNum(StatusHeight));
        make.height.mas_equalTo(kScaleNum(44));
        make.right.mas_equalTo(0);
        make.left.mas_equalTo(kScaleNum(0));
    }];
    
    [self.iconImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(29, 29));
    }];
    
    [self.startLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.startDateLab);
        make.top.mas_equalTo(kScaleNum(2));
        make.height.mas_equalTo(kScaleNum(20));
    }];
    
    [self.startDateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.iconImageV.mas_left).offset(kScaleNum(-68));
        make.top.mas_equalTo(kScaleNum(22));
        make.height.mas_equalTo(kScaleNum(20));
    }];
    
    [self.endLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.endDateLab);
        make.top.mas_equalTo(kScaleNum(2));
        make.height.mas_equalTo(kScaleNum(20));
    }];
    
    [self.endDateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageV.mas_right).offset(kScaleNum(68));
        make.top.mas_equalTo(kScaleNum(22));
        make.height.mas_equalTo(kScaleNum(20));
    }];
    
    [self.startDateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.startDateLab);
        make.top.bottom.mas_equalTo(0);
    }];
    
    [self.endDateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.endDateLab);
        make.top.bottom.mas_equalTo(0);
    }];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *timeFormatStr = [dateFormatter stringFromDate:[NSDate date]];
    _startDateLab.text = timeFormatStr;
    _endDateLab.text = timeFormatStr;
    
//    [self.view addSubview:self.headerView];
    
    [self.view addSubview:self.shopView];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.tipView];
    
    UILabel *allShopLab = [[UILabel alloc] init];
    allShopLab.text =[SAValueManger shareInStance].userInfo.storeName;
    allShopLab.font = kBFont(15);
    allShopLab.textColor = kBlackColor;
    [self.shopView addSubview:allShopLab];
    self.allShopLab = allShopLab;
    
    UIImageView *allowImageV2 = [[UIImageView alloc] init];
    allowImageV2.image = [UIImage imageNamed:@"bule_downAllow"];
    [self.shopView addSubview:allowImageV2];
    allowImageV2.contentMode = UIViewContentModeCenter;
    self.allowImageV2 = allowImageV2;
    
    UIButton *allowBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [allowBtn2 addTarget:self action:@selector(clickedAllowBtn2) forControlEvents:UIControlEventTouchUpInside];
    [self.shopView addSubview:allowBtn2];
    
    [allShopLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(kScaleNum(25));
        make.height.mas_equalTo(kScaleNum(35));
        make.width.mas_lessThanOrEqualTo(kScaleNum(150));
        make.width.mas_greaterThanOrEqualTo(kScaleNum(50));
    }];
    
    [allowImageV2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(kScaleNum(35));
        make.width.mas_equalTo(kScaleNum(55));
        make.left.mas_equalTo(allShopLab.mas_right).offset(kScaleNum(-10));
    }];
    
    [allowBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(0);
        make.height.mas_equalTo(kScaleNum(35));
        make.right.mas_equalTo(allShopLab.mas_right).offset(kScaleNum(30));
    }];

    UILabel *statusLab = [[UILabel alloc] init];
    statusLab.text = @"未处理";
    statusLab.font = kBFont(15);
    statusLab.textColor = kBlackColor;
    [self.shopView addSubview:statusLab];
    self.statusLab = statusLab;
    
    UIImageView *statusImageV = [[UIImageView alloc] init];
    statusImageV.image = [UIImage imageNamed:@"bule_downAllow"];
    [self.shopView addSubview:statusImageV];
    statusImageV.contentMode = UIViewContentModeCenter;
    self.statusImageV = statusImageV;
    
    UIButton *statusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [statusBtn addTarget:self action:@selector(clickedStatusBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.shopView addSubview:statusBtn];
    
    [statusLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(kScaleNum(250));
        make.height.mas_equalTo(kScaleNum(35));
        make.width.mas_equalTo(kScaleNum(55));
    }];
    
    [statusImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(kScaleNum(35));
        make.width.mas_equalTo(kScaleNum(55));
        make.left.mas_equalTo(statusLab.mas_right).offset(kScaleNum(-10));
    }];
    
    [statusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(0);
        make.height.mas_equalTo(kScaleNum(35));
        make.left.mas_equalTo(statusLab.mas_left).offset(kScaleNum(30));
    }];
    
    self.currentPage = 1;
    self.stockInvoiceListAPI.startTime = timeFormatStr;
    self.stockInvoiceListAPI.endTime = timeFormatStr;
    self.stockInvoiceListAPI.pageSize = 20;
    self.stockInvoiceListAPI.merchantId = @([SAValueManger shareInStance].userInfo.merchantId).stringValue;
    [self loadData];
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage = 1;
        [weakSelf loadData];
    }];
    MJRefreshAutoStateFooter *mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage ++;
        [weakSelf loadData];
    }];
    [mj_footer setTitle:@"已经到底啦~" forState:MJRefreshStateIdle];//设置闲置状态下不显示“点击或上拉加载更多”
    self.tableView.mj_footer = mj_footer;
}

#pragma mark - lazy
- (UIView *)topContentView {
    if (!_topContentView) {
        _topContentView = [[UIView alloc] init];
        _topContentView.backgroundColor = [UIColor whiteColor];
    }
    return _topContentView;
}

- (UILabel *)startLab {
    if (!_startLab) {
        _startLab = [[UILabel alloc] init];
        _startLab.textColor = kBlackColor;
        _startLab.font = kFont(15);
        _startLab.text = @"开始";
    }
    return _startLab;
}

- (UILabel *)startDateLab {
    if (!_startDateLab) {
        _startDateLab = [[UILabel alloc] init];
        _startDateLab.textColor = kBlackColor;
        _startDateLab.font = kFont(15);
    }
    return _startDateLab;
}

- (UILabel *)endLab {
    if (!_endLab) {
        _endLab = [[UILabel alloc] init];
        _endLab.textColor = kBlackColor;
        _endLab.font = kFont(15);
        _endLab.text = @"截止";
    }
    return _endLab;
}

- (UILabel *)endDateLab {
    if (!_endDateLab) {
        _endDateLab = [[UILabel alloc] init];
        _endDateLab.textColor = kBlackColor;
        _endDateLab.font = kFont(15);
    }
    return _endDateLab;
}

- (UIImageView *)iconImageV {
    if (!_iconImageV) {
        _iconImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SA_to"]];
    }
    return _iconImageV;
}

- (UIButton *)startDateBtn {
    if (!_startDateBtn) {
        _startDateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_startDateBtn addTarget:self action:@selector(clickStartDateBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _startDateBtn;
}

- (UIButton *)endDateBtn {
    if (!_endDateBtn) {
        _endDateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_endDateBtn addTarget:self action:@selector(clickEndDateBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _endDateBtn;
}

-(UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 79), kScreenWidth, kScaleNum(35))];
        
        UIView *whireView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScaleNum(35))];
        whireView2.backgroundColor = kTextBgColor;
        [_headerView addSubview:whireView2];
        
        UILabel *titleLab1 = [[UILabel alloc] initWithFrame:SARectMake(20, 0, 71, 35)];
        titleLab1.text = @"单号";
        titleLab1.textColor = kTextColor;
        titleLab1.font = kBFont(14);
        titleLab1.textAlignment = NSTextAlignmentLeft;
        [whireView2 addSubview:titleLab1];
        
        UILabel *titleLab2 = [[UILabel alloc] initWithFrame:SARectMake(92, 0, 65, 35)];
        titleLab2.text = @"盘前";
        titleLab2.textColor = kTextColor;
        titleLab2.font = kBFont(14);
        titleLab2.textAlignment = NSTextAlignmentCenter;
        [whireView2 addSubview:titleLab2];
        
        UILabel *titleLab3 = [[UILabel alloc] initWithFrame:SARectMake(162, 0, 65, 35)];
        titleLab3.text = @"盘后";
        titleLab3.textColor = kTextColor;
        titleLab3.font = kBFont(14);
        titleLab3.textAlignment = NSTextAlignmentCenter;
        [whireView2 addSubview:titleLab3];
        
        UILabel *titleLab4 = [[UILabel alloc] initWithFrame:SARectMake(232, 0, 65, 35)];
        titleLab4.text = @"盘亏";
        titleLab4.textColor = kTextColor;
        titleLab4.font = kBFont(14);
        titleLab4.textAlignment = NSTextAlignmentCenter;
        [whireView2 addSubview:titleLab4];
        
        UILabel *titleLab5 = [[UILabel alloc] initWithFrame:SARectMake(303, 0, 65, 35)];
        titleLab5.text = @"盘点人";
        titleLab5.textColor = kTextColor;
        titleLab5.font = kBFont(14);
        titleLab5.textAlignment = NSTextAlignmentCenter;
        [whireView2 addSubview:titleLab5];
    }
    return _headerView;
}

-(UIView *)shopView {
    if (!_shopView) {
        _shopView = [[UIView alloc] initWithFrame:SARectMake(0, StatusHeight + 44, kBaseScreenWidth, 35)];
        _shopView.backgroundColor = kLineColor;
        
    }
    return _shopView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 79), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight + 79)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SAInventoryListCell class] forCellReuseIdentifier:@"SAInventoryListCell"];
    }
    return _tableView;
}

-(UIView *)tipView {
    if (!_tipView) {
        _tipView = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 79), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight + 79))];
        _tipView.backgroundColor = [UIColor whiteColor];
        UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Shop_search2"]];
        icon.frame = SARectMake(124, 106, 138, 83);
        [_tipView addSubview:icon];
        UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, icon.maxY + kScaleNum(20), _tipView.width, kScaleNum(20))];
        titleLab.text = @"暂无相关信息";
        titleLab.textAlignment = NSTextAlignmentCenter;
        titleLab.textColor = kGrayColor;
        [_tipView addSubview:titleLab];
    }
    return _tipView;
}

-(SAStockInvoiceListAPI *)stockInvoiceListAPI {
    if (!_stockInvoiceListAPI) {
        _stockInvoiceListAPI = [SAStockInvoiceListAPI apiWithDelegate:self];
    }
    return _stockInvoiceListAPI;
}

-(NSMutableArray<SAInventoryListModel2 *> *)dataMarr {
    if (!_dataMarr) {
        _dataMarr = [NSMutableArray array];
    }
    return _dataMarr;
}

-(NSDateFormatter *)dateFormatter {
    if (!_dateFormatter) {
        _dateFormatter = [NSDateFormatter new];
        [_dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }
    return _dateFormatter;
}

-(SASystemStoreStoreListAPI *)systemStoreStoreListAPI {
    if (!_systemStoreStoreListAPI) {
        _systemStoreStoreListAPI = [SASystemStoreStoreListAPI apiWithDelegate:self];
    }
    return _systemStoreStoreListAPI;
}

-(NSMutableArray<SAStoreModel *> *)storeList {
    if (!_storeList) {
        _storeList = [NSMutableArray array];
    }
    return _storeList;
}

-(SAStockCheckHandleAPI *)stockCheckHandleAPI {
    if (!_stockCheckHandleAPI) {
        _stockCheckHandleAPI = [SAStockCheckHandleAPI apiWithDelegate:self];
    }
    return _stockCheckHandleAPI;
}

@end
